package com.btpj.module_main.ui.widgets.tag_flow

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.btpj.jetpack.widgets.FlowLayout

/**
 * 封装Tag的流式布局
 *
 * @author LTP 2020/7/13
 */
class TagFlowLayout(context: Context, attributeSet: AttributeSet) :
    FlowLayout(context, attributeSet), NotifyDataSetChangedListener {

    private lateinit var mAdapter: TagFlowAdapter

    fun setAdapter(adapter: TagFlowAdapter) {
        mAdapter = adapter
        mAdapter.setNotifyDataSetChangedListener(this)
        onDataChanged()
    }

    override fun onDataChanged() {
        removeAllViews()
        for (i in 0 until mAdapter.getItemCount()) {
            val view = mAdapter.createView(LayoutInflater.from(context), this, i)
            mAdapter.bindView(view, i)
            addView(view)
            view.setOnClickListener { mAdapter.onItemViewClick(view, i) }
        }
    }


}